package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.google.gson.Gson;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ApphomeCardViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.HomeWeathRes;
import sz.szsmk.citizencard.ui.adapter.bean.AppHomeCardBean;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.StringUtils;
import sz.szsmk.citizencard.widget.BlurringView;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/18 17:10
 * description:
 */
public class AppHomeCardAdapter extends DelegateAdapter.Adapter<AppHomeCardAdapter.AppHomeCardHolder> {
    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private AppHomeCardBean appHomeCardBean;
    private RelativeLayout backView;
    private AppHomeCardHolder holder;
    private BlurringView mBlurringView;
    private Boolean isLogin;
    private HomeWeathRes weathData;
    private HomeWeathRes picData;
    private View mCurrentView;

    public AppHomeCardAdapter(FragmentActivity activity, DefaultLayoutHelper headHelper, RelativeLayout backView) {
        mContext = activity;
        mGridLayoutHelper = headHelper;
        this.backView = backView;
        isLogin = false;
    }

    public void setPrimaryItem(ViewGroup container, int position, Object object) {
//        super.setPrimaryItem(container, position, object);
        mCurrentView = (View)object;
    }
    
    public View getPrimaryItem() {
        return mCurrentView;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_card_view;
    }

    @Override
    public AppHomeCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return AppHomeCardHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(AppHomeCardHolder holder, final int position) {

        ApphomeCardViewBinding binding = holder.binding;
        if (picData != null) {

            mBlurringView = binding.blurringView1;
            mBlurringView.setBlurredView(backView);
            mBlurringView.invalidateData();
        }
        String access_token = ACache.get(mContext).getAsString("access_token");
        if (isLogin) {
            if (appHomeCardBean != null) {
                String level = appHomeCardBean.getLevel();
                if (level.equals("L1")) {
                    binding.apphomeCardIslogin.setVisibility(View.GONE);
                    binding.apphomeCardUnlogin.setVisibility(View.VISIBLE);
                    binding.apphomeCardUnlogin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            JumpUtils.jump(mContext, "RealNameActivity", 0);
                        }
                    });
                } else {
                    binding.apphomeCardIslogin.setVisibility(View.VISIBLE);
                    binding.apphomeCardUnlogin.setVisibility(View.GONE);
                    binding.apphomeCardGhf.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //http://47.100.247.204:8083/portal/osmanthus/osmanthus_index.html
                            if (ghfInterface != null) {
                                ghfInterface.onClick(view, position);
                            }
                        }
                    });
                    binding.apphomeRealnameLl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("appHomeCardBean", appHomeCardBean);
                            JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                        }
                    });

                    binding.apphomeCardGorealname.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (isLogin) {
                                JumpUtils.jump(mContext, "RealNameActivity", 0);
                            } else {
                                JumpUtils.jump(mContext, "login", 0);
                            }
                        }
                    });
                    if (appHomeCardBean == null) {
//            Toast.makeText(mContext, "请先进行进行实名认证", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!StringUtils.isBlank(appHomeCardBean.getCustName())) {
                        binding.apphomeCardName.setText(appHomeCardBean.getCustName() + "，您好！");
                    } else {
                        binding.apphomeCardName.setText("先生，您好！");
                    }
                    if (!StringUtils.isBlank(appHomeCardBean.getEcityCardNo()) && appHomeCardBean.getEcityCardNo().length() > 4) {
                        binding.apphomeCardDzsmk.setText(appHomeCardBean.getEcityCardNo().substring(0, 4) +
                                " **** " + appHomeCardBean.getEcityCardNo().substring(
                                appHomeCardBean.getEcityCardNo().length() - 4,
                                appHomeCardBean.getEcityCardNo().length()));
                    } else {
                        binding.apphomeCardDzsmk.setText("");
                    }
                    if (!StringUtils.isBlank(appHomeCardBean.getLevel())) {
                        binding.level.setText(appHomeCardBean.getLevel());
                        switch (appHomeCardBean.getLevel()) {
                            case "L1":
                                binding.levelDesc.setText("已完成手机号认证");
                                break;
                            case "L2":
                                binding.levelDesc.setText("已完成实名认证");
                                break;
                            case "L3":
                                String levelDesc = appHomeCardBean.getLevelDesc();
                                binding.apphomeUpdataTv.setVisibility(View.GONE);
                                binding.levelDesc.setText("已完成L3认证");
                                break;
                        }
                    }
                    binding.apphomeCardEwm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {



                            goIdentityCodeActivity();

                        }


                    });
                    binding.cardView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("L2".equals(appHomeCardBean.getLevel()) || "L3".equals(appHomeCardBean.getLevel())) {
//                                JumpUtils.gotoNeedGesture(mContext, "elecCitizenCard");

                                if (Constant.isOpen.equals("1")) {

                                    Bundle bundle = new Bundle();
                                    bundle.putString(Constant.ID_CARD, appHomeCardBean.getIdCard());
                                    bundle.putString(Constant.ID_NAME, appHomeCardBean.getCustName());
                                    JumpUtils.gotoNeedGesture(mContext, "elecCitizenCard", bundle);

                                } else {

                                    JumpUtils.jump(mContext, "elecCitizenCard", 0);
                                }
                            }
                        }
                    });

                }
            }

        } else {
            binding.apphomeCardIslogin.setVisibility(View.GONE);
            binding.apphomeCardUnlogin.setVisibility(View.VISIBLE);
            binding.apphomeCardUnlogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    JumpUtils.jump(mContext, "login", 0);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setPicData(HomeWeathRes picData) {
        this.picData = picData;
    }


    public static class AppHomeCardHolder extends RecyclerView.ViewHolder {

        private ApphomeCardViewBinding binding;

        public static AppHomeCardHolder creat(ViewGroup parent) {

            ApphomeCardViewBinding binding =
                    ApphomeCardViewBinding.inflate(LayoutInflater.from((parent.getContext())), parent, false);


            return new AppHomeCardHolder(binding);
        }

        public AppHomeCardHolder(ApphomeCardViewBinding binding) {
            super(binding.getRoot());

            this.binding = binding;

        }

    }

    public void goIdentityCodeActivity() {
        if ("L2".equals(appHomeCardBean.getLevel()) || "L3".equals(appHomeCardBean.getLevel())) {
            if (Constant.isOpen.equals("1")) {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.ID_CARD, appHomeCardBean.getIdCard());
                bundle.putString(Constant.ID_NAME, appHomeCardBean.getCustName());
                JumpUtils.gotoNeedGesture(mContext, "identityCodeActivity", bundle);
            } else {
                String idCard = appHomeCardBean.getIdCard();
                Bundle bundle = new Bundle();
                bundle.putString(Constant.ID_CARD, idCard);
                bundle.putString(Constant.ID_NAME, appHomeCardBean.getCustName());
                JumpUtils.jumptoNatvie(mContext, "identityCodeActivity", 0, bundle);
            }
        }
    }

    public void invalidateData() {
//        holder.blurringView.invalidateData();
        if(mBlurringView!=null){
            mBlurringView.invalidateData();
        }

    }

    public void setAppHomeCardBean(AppHomeCardBean appHomeCardBean) {
        this.appHomeCardBean = appHomeCardBean;
        String json = new Gson().toJson(this.appHomeCardBean);
        notifyDataSetChanged();
    }

    public void setLogin(Boolean login) {
        isLogin = login;
        notifyDataSetChanged();
    }


    public interface GhfInterface {
        public void onClick(View view, int position);
    }

    public void GhfOnClick(GhfInterface ghfInterface) {
        this.ghfInterface = ghfInterface;
    }

    private GhfInterface ghfInterface;

}
